
#include "parameter.h"
#include "photo_func.h"
#include "video_func.h"
#include "music_func.h"

int main()
{
    // 显示菜单
    //  打开LCD屏
    int lcd_fd = openfd("/dev/fb0", O_RDWR);

    // 3.申请映射内存
    int *lcd_p = mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, lcd_fd, 0);
    if (lcd_p == NULL)
        perror("mmap error");

    // 打开触摸屏
    int ts_fd = openfd("/dev/input/event0", O_RDONLY);

    // 触摸屏参数
    int x = 0;
    int y = 0;
    struct input_event touch;

    // 控制打印
    int flag_menu = 0;

    // 菜单
    printf("打印菜单\n");
    while (1)
    {
        if (flag == MENU)
        {
            if (flag_menu == 0)
            {
                show_bmp("back.bmp", lcd_fd, 0, 0);
                show_bt_bmp("shutdown1.bmp", lcd_fd, 0, 409);
                flag_menu = 1;
            }

            read(ts_fd, &touch, sizeof(touch)); // 没触摸的时候。read是堵塞的
            if (touch.type == EV_ABS && touch.code == ABS_X)
                x = touch.value * 800 / 1024; // 判断是不是触摸屏设备，在判断是不是x坐标，
            if (touch.type == EV_ABS && touch.code == ABS_Y)
                y = touch.value * 480 / 600;
            if (touch.type == EV_KEY && touch.code == BTN_TOUCH && touch.value == 0) // 判断是否手离开
            {
                // 进入功能
                if ((x >= 165 && x <= 235) && (y >= 205 && y <= 275)) // photo功能
                {
                    printf("photo功能\n");
                    flag = PHOTO;
                    pthread_t photo_id;
                    int n = pthread_create(&photo_id, NULL, photo_func, "photo_pthread");
                    if (n != 0)
                        perror("photo pthread create error");
                    flag_menu = 0;
                }
                else if ((x >= 365 && x <= 435) && (y >= 205 && y <= 275)) // music功能
                {
                    printf("music功能\n");
                    flag = MUSIC;
                    pthread_t music_id;
                    int n = pthread_create(&music_id, NULL, music_func, "music_pthread");
                    if (n != 0)
                        perror("music pthread create error");
                    flag_menu = 0;
                }
                else if ((x >= 565 && x <= 635) && (y >= 205 && y <= 275)) // video功能
                {
                    printf("video功能\n");
                    flag = VIDEO;
                    pthread_t video_id;
                    int n = pthread_create(&video_id, NULL, video_func, "video_pthread");
                    if (n != 0)
                        perror("video pthread create error");
                    flag_menu = 0;
                }
                else if ((x >= 0 && x <= 70) && y >= 409) // 退出
                {
                    break;
                }
            }
        }
    }

    for (int y = 0; y < 480; y++)
    {
        for (int x = 0; x < 800; x++)
        {
            *(lcd_p + (800 * y + x)) = 0x00000000;
        }
    }

    close(lcd_fd);
    close(ts_fd);
    munmap(lcd_p, 800 * 480 * 4);

    return 0;
}